import bianliang from "./bianliang.js";
import Boom from "./Boom.js";

class Enemy {
	constructor() {
		var temp = parseInt(Math.random() * 100);
		if (temp >= 95) {
			this.type = 2;//大飞机
			this.speed = 3;
			this.life = 10;
			this.score = 50;
			this.img = bianliang.resObj[5];
		} else if(temp >= 85) {
			this.type =1;//中飞机
			this.speed = 4;
			this.life = 3;
			this.score = 30;
			this.img = bianliang.resObj[4];
		} else {
			this.type = 0;//小飞机
			this.speed = 5;
			this.life = 1;
			this.score = 10;
			this.img = bianliang.resObj[3];
		}
	    this. x = parseInt(Math.random() * 480);
		this.width = this.img.width;
		this.height = this.img.height;
		if (this.x >= 480 - this.width) {
			this.x = 480 - this.width
		}
		this. y = this.height * (-1);
	}
	
	draw(ctx) {
		this.move();
		ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
	}
	
	move() {
		this.y += this.speed;
		if (this.y >= 850) {
			var index = bianliang.enemyList.indexOf(this);
			bianliang.enemyList.splice(index, 1);
		}
	}
	
	//判断当前敌机是否死亡
	isDie() {
		if (this.life <= 0) {
			//说明这个飞机已经死了
			//飞机在哪里死亡，爆炸就在哪里产生
			var b = new Boom(this.x, this.y, this.type);
			bianliang.boomList.push(b);
			bianliang.score += this.score;
			var index = bianliang.enemyList.indexOf(this);
			bianliang.enemyList.splice(index, 1);
		} else {
			//说明飞机只是中弹但是没有死，这时候我们应该换一个破损的飞机图片
			if (this.type == 1) {
				//中飞机
				this.img = bianliang.resObj[8];
			} else if (this.type == 2) {
				//大飞机
				this.img = bianliang.resObj[9];
			}
		}
	}
}

export default Enemy;